package v1c07;

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;

/**
 * @author gjunming@gmail.com
 * @version Aug 6, 2009 2:13:20 PM
 */
public class LoadImage {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ImageFrame imageFrame = new ImageFrame();
		imageFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		imageFrame.setVisible(true);
	}

}

class ImageFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	private static final int FWIDTH = 300;
	private static final int FHEIGHT = 200;

	public ImageFrame() {
		setSize(FWIDTH, FHEIGHT);
		ImageComponent imageComponent = new ImageComponent();
		add(imageComponent);
	}
}

class ImageComponent extends JComponent {

	private static final long serialVersionUID = 1L;
	private static final int FILE_LEFTX = 0;
	private static final int FILE_TOPY = 0;
	private static final int URL_LEFTX = 65;
	private static final int URL_TOPY = 65;

	@Override
	public void paintComponent(Graphics g) {
		Image fileImage = null;
		try {
			fileImage = ImageIO
					.read(new File(
							"E:\\javaprojects\\corejava_excise\\bin\\v1c07\\example.gif"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		g.drawImage(fileImage, FILE_LEFTX, FILE_TOPY, null);

		Image urlImage = null;
		try {
			urlImage = ImageIO.read(new URL(
					"http://qa/output/images/kisterslogo_weiss_blau.gif"));
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		g.drawImage(urlImage, URL_LEFTX, URL_TOPY, null);

		// full screen with one image
		for (int i = 0; i * fileImage.getWidth(this) <= getWidth(); i++) {
			for (int j = 0; j * fileImage.getHeight(this) <= getHeight(); j++) {
				if (i + j > 0) {
					g.copyArea(0, 0, fileImage.getWidth(this), fileImage
							.getHeight(this), i * fileImage.getWidth(this), j
							* fileImage.getHeight(this));
				}
			}
		}

	}
}
